package controller;

import helper.UserSession;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import databeans.Customer;
import databeans.Employee;
import databeans.Fund;
import databeans.Position;
import model.EmployeeDAO;
import model.FundDAO;
import model.Model;
import model.PositionDAO;

public class CustomerViewFundsAction extends Action{
    private EmployeeDAO employeeDAO;
    private FundDAO fundDAO;
    private PositionDAO positionDAO;

    public CustomerViewFundsAction(Model model) {
        employeeDAO = model.getEmployeeDAO();
        fundDAO = model.getFundDAO();
        positionDAO = model.getPositoinDAO();
    }
    
    public String getName() {
        return "customerViewCustomerFunds.do";
    }
    

    public String perform(HttpServletRequest request) {
        //ATTENTION
        // define target page
        String loginPage = "customer/customer-login.jsp";
        String successPage = "customer/view-customer-funds.jsp"; 
        
        //set the employee
        Employee employee = null;
        Customer customer = null;
        Fund fund = null;

        //errors and messages will be stored here
        //resetting those containers
        List<String> errors = new ArrayList<String>();
        List<String> messages = new ArrayList<String>();
   
        //define the error and message
        request.setAttribute("errors", errors);
        request.setAttribute("messages", messages);
        request.setAttribute("page", "my-funds");
         
        //check if employee has been logged in
        //request.getSession() will return the current session and if one does not exist, a new session will be cretaed.
        //request.getSession(true) will return the current session if one exists, if one doesn't exits a new one will be created.
        //So there is actually no difference between the two methods HOWEVER, if you use request.getSession(false), it will return the current session if one exists and if one DOES NOT exist a new one will NOT be cretaed.
        employee = UserSession.getLoggedInEmployee(request);
        if (employee != null) {
            UserSession.removeEmployeeFromSession(request);
            errors.add("This section is closed for employee");
            return loginPage ;
        }
        
        //check if customer has actually logged in
        customer = UserSession.getLoggedInCustomer(request);
        if (customer == null) {
            errors.add("You need to log in");
            return loginPage;
        }
    
        try {
            
            Position [] positionList = positionDAO.getPositions(customer.getCustomerID());
            HttpSession session = request.getSession(true); 
            
            if (positionList == null) {
                messages.add("There is no position for customer "+ customer.getFirstName() + " currently available to be seen");
                return successPage;
            }else
            {
            	List<String> fundNames = new ArrayList<String>();
            	
            	System.out.println("Num of positions found = " + positionList.length);
            	
            	for(int i =0;i< positionList.length;i++)
            	{
            		int fundID = positionList[i].getFundID();
            		System.out.println("fund ID = " + fundID);
            		String name = fundDAO.lookup(fundID).getName();
            		fundNames.add(name);
            		System.out.println("Fund name =" + name);
            	}
            	request.setAttribute("fundNames", fundNames);
            }
            

            // Attach (this copy of) the employee bean to the session
            
            session.setAttribute("customer", customer);
            request.setAttribute("positionList", positionList);
            
            //set the welcome message
            //messages.add("Hi " + customer.getFirstName() + "!, belows are some stuff you owned");
            return successPage;

        } catch (Exception e) {
            errors.add("Unable to retrieve customer's position");
            return successPage;
        }

    }
}